package top.oylan.manager.service.impl;

import org.springframework.stereotype.Service;
import top.oylan.manager.entity.Action;
import top.oylan.manager.entity.extend.ActionExtend;
import top.oylan.manager.entity.example.ActionExample;
import top.oylan.manager.mapper.ActionMapper;
import top.oylan.manager.mapper.RoleActionMapper;
import top.oylan.manager.service.ActionService;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-02-11
 */
@Service("actionService")
public class ActionServiceImpl implements ActionService {

    @Resource
    ActionMapper mapper;

    @Resource
    RoleActionMapper roleActionMapper;

    @Override
    public List<Action> selectActionByParentId(Integer parentId) {

        ActionExample actionExample = new ActionExample();
        ActionExample.Criteria criteria = actionExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        return mapper.selectByExample(actionExample);
    }

    @Override
    public List<ActionExtend> selectAllActions() {
        return mapper.selectAllActions();
    }

    @Override
    public Action selectActionById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Action action) throws SQLException {
        mapper.updateByPrimaryKeySelective(action);
    }

    @Override
    public void add(Action action) throws SQLException{
        mapper.insertSelective(action);
    }

    @Override
    public void delete(Integer id) throws Exception {
        mapper.deleteByPrimaryKey(id);
    }

    public List<Action> selectActionsByParentIdAndUserId(Integer parentId, Integer userId){
        Map map = new HashMap<String,Object>();
        map.put("parentId",parentId);
        map.put("userId",userId);
        return mapper.selectByParentIdWithPerms(map);
    }

    @Override
    public List<ActionExtend> selectAllActionsWithAuth(Integer roleId) {
        return mapper.selectAllActionsWithAuth(roleId);
    }

    @Override
    public int deletePermsByRoleId(Integer roleId) {
        return roleActionMapper.deleteByRoleId(roleId);
    }

    @Override
    public int savePerm(Integer roleId, List<Integer> actions) {
        return roleActionMapper.saveRoleActions(roleId,actions);
    }
}
